package com.chengjue.transaction.learn.thread.countdownlatch;

import com.google.common.util.concurrent.ThreadFactoryBuilder;

import java.util.concurrent.*;

/**
 * 描述:
 *
 * @author Kipeng Huang
 * @create 2018-03-03 下午1:32
 */
public class TestCountDownLatch {

	public static void main(String[] args) throws InterruptedException {
		CountDownLatch countDownLatch = new CountDownLatch(20);
		//可以定义线程名称规则
		ThreadFactory namedThreadFactory = new ThreadFactoryBuilder()
				.setNameFormat("demo-pool-%d").build();
		//使用阿里Java规范自己创建线程池
		ExecutorService threadPool = new ThreadPoolExecutor(4,
				20,
				1000L,
				TimeUnit.MILLISECONDS,
				new ArrayBlockingQueue<Runnable>(1024),
				namedThreadFactory,
				new ThreadPoolExecutor.AbortPolicy());
		for (int i = 0; i < 20; i++) {
			threadPool.execute(new CountRunnable(countDownLatch));
		}
		countDownLatch.await();
		System.out.println("全部数据处理完毕");
		threadPool.shutdown();
	}
}
